{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style=\"color:blue\" align=\"center\">Pandas Time Series Analysis: Period and PeriodIndex</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Yearly Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016', 'A-DEC')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "y = pd.Period('2016')\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-01 00:00:00')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-12-31 23:59:59.999999999')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.end_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.is_leap_year"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Monthly Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-12', 'M')"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = pd.Period('2017-12')\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2017-12-01 00:00:00')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2017-12-31 23:59:59.999999999')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.end_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2018-01', 'M')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Daily Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-02-28', 'D')"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = pd.Period('2016-02-28', freq='D')\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-02-28 00:00:00')"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-02-28 23:59:59.999999999')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.end_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-02-29', 'D')"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Hourly Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-08-15 23:00', 'H')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h = pd.Period('2017-08-15 23:00:00',freq='H')\n",
    "h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-08-16 00:00', 'H')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Achieve same results using pandas offsets hour</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-08-16 00:00', 'H')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h+pd.offsets.Hour(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Quarterly Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017Q1', 'Q-JAN')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1= pd.Period('2017Q1', freq='Q-JAN')\n",
    "q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-02-01 00:00:00')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1.start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-04-30 23:59:59.999999999')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1.end_time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Use asfreq to convert period to a different frequency</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-02', 'M')"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1.asfreq('M',how='start')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-04', 'M')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q1.asfreq('M',how='end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Weekly Period</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-07-03/2017-07-09', 'W-SUN')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = pd.Period('2017-07-05',freq='W')\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-06-26/2017-07-02', 'W-SUN')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-08-14/2017-08-20', 'W-SUN')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2 = pd.Period('2017-08-15',freq='W')\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2-w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">PeriodIndex and period_range</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2011Q1', '2011Q2', '2011Q3', '2011Q4', '2012Q1', '2012Q2',\n",
       "             '2012Q3', '2012Q4', '2013Q1', '2013Q2', '2013Q3', '2013Q4',\n",
       "             '2014Q1', '2014Q2', '2014Q3', '2014Q4', '2015Q1', '2015Q2',\n",
       "             '2015Q3', '2015Q4', '2016Q1', '2016Q2', '2016Q3', '2016Q4',\n",
       "             '2017Q1'],\n",
       "            dtype='period[Q-DEC]', freq='Q-DEC')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = pd.period_range('2011', '2017', freq='q')\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2011-01-01 00:00:00')"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r[0].start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2011-03-31 23:59:59.999999999')"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r[0].end_time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Walmart's fiscal year ends in Jan, below is how you generate walmart's fiscal quarters between 2011 and 2017**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2011Q4', '2012Q1', '2012Q2', '2012Q3', '2012Q4', '2013Q1',\n",
       "             '2013Q2', '2013Q3', '2013Q4', '2014Q1', '2014Q2', '2014Q3',\n",
       "             '2014Q4', '2015Q1', '2015Q2', '2015Q3', '2015Q4', '2016Q1',\n",
       "             '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2', '2017Q3',\n",
       "             '2017Q4'],\n",
       "            dtype='period[Q-JAN]', freq='Q-JAN')"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = pd.period_range('2011', '2017', freq='q-jan')\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-11-01 00:00:00')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r[0].start_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2011-01-31 23:59:59.999999999')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r[0].end_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01', '2016-04', '2016-07', '2016-10', '2017-01', '2017-04',\n",
       "             '2017-07', '2017-10', '2018-01', '2018-04'],\n",
       "            dtype='period[3M]', freq='3M')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = pd.PeriodIndex(start='2016-01', freq='3M', periods=10)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -0.895267\n",
       "2016-04    1.343498\n",
       "2016-07   -0.979625\n",
       "2016-10   -0.292720\n",
       "2017-01    0.275139\n",
       "2017-04    1.200450\n",
       "2017-07    1.890607\n",
       "2017-10    0.259646\n",
       "2018-01   -1.113016\n",
       "2018-04    1.669858\n",
       "Freq: 3M, dtype: float64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "ps = pd.Series(np.random.randn(len(idx)), idx)\n",
    "ps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Partial Indexing</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -0.895267\n",
       "2016-04    1.343498\n",
       "2016-07   -0.979625\n",
       "2016-10   -0.292720\n",
       "2017-01    0.275139\n",
       "Freq: 3M, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps['2016']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -0.895267\n",
       "2016-04    1.343498\n",
       "2016-07   -0.979625\n",
       "2016-10   -0.292720\n",
       "2017-01    0.275139\n",
       "2017-04    1.200450\n",
       "2017-07    1.890607\n",
       "2017-10    0.259646\n",
       "Freq: 3M, dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps['2016':'2017']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Converting between representations</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-01   -0.895267\n",
       "2016-04-01    1.343498\n",
       "2016-07-01   -0.979625\n",
       "2016-10-01   -0.292720\n",
       "2017-01-01    0.275139\n",
       "2017-04-01    1.200450\n",
       "2017-07-01    1.890607\n",
       "2017-10-01    0.259646\n",
       "2018-01-01   -1.113016\n",
       "2018-04-01    1.669858\n",
       "Freq: QS-OCT, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pst = ps.to_timestamp()\n",
    "pst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-01-01', '2016-04-01', '2016-07-01', '2016-10-01',\n",
       "               '2017-01-01', '2017-04-01', '2017-07-01', '2017-10-01',\n",
       "               '2018-01-01', '2018-04-01'],\n",
       "              dtype='datetime64[ns]', freq='QS-OCT')"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pst.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016Q1   -0.895267\n",
       "2016Q2    1.343498\n",
       "2016Q3   -0.979625\n",
       "2016Q4   -0.292720\n",
       "2017Q1    0.275139\n",
       "2017Q2    1.200450\n",
       "2017Q3    1.890607\n",
       "2017Q4    0.259646\n",
       "2018Q1   -1.113016\n",
       "2018Q2    1.669858\n",
       "Freq: Q-DEC, dtype: float64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps = pst.to_period()\n",
    "ps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2',\n",
       "             '2017Q3', '2017Q4', '2018Q1', '2018Q2'],\n",
       "            dtype='period[Q-DEC]', freq='Q-DEC')"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"color:purple\">Processing Wal Mart's Financials</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Line Item</th>\n",
       "      <th>2017Q1</th>\n",
       "      <th>2017Q2</th>\n",
       "      <th>2017Q3</th>\n",
       "      <th>2017Q4</th>\n",
       "      <th>2018Q1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Revenue</td>\n",
       "      <td>115904</td>\n",
       "      <td>120854</td>\n",
       "      <td>118179</td>\n",
       "      <td>130936</td>\n",
       "      <td>117542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Expenses</td>\n",
       "      <td>86544</td>\n",
       "      <td>89485</td>\n",
       "      <td>87484</td>\n",
       "      <td>97743</td>\n",
       "      <td>87688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Profit</td>\n",
       "      <td>29360</td>\n",
       "      <td>31369</td>\n",
       "      <td>30695</td>\n",
       "      <td>33193</td>\n",
       "      <td>29854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Line Item  2017Q1  2017Q2  2017Q3  2017Q4  2018Q1\n",
       "0   Revenue  115904  120854  118179  130936  117542\n",
       "1  Expenses   86544   89485   87484   97743   87688\n",
       "2    Profit   29360   31369   30695   33193   29854"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv(\"wmt.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Line Item</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Profit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017Q1</th>\n",
       "      <td>115904</td>\n",
       "      <td>86544</td>\n",
       "      <td>29360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q2</th>\n",
       "      <td>120854</td>\n",
       "      <td>89485</td>\n",
       "      <td>31369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q3</th>\n",
       "      <td>118179</td>\n",
       "      <td>87484</td>\n",
       "      <td>30695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q4</th>\n",
       "      <td>130936</td>\n",
       "      <td>97743</td>\n",
       "      <td>33193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018Q1</th>\n",
       "      <td>117542</td>\n",
       "      <td>87688</td>\n",
       "      <td>29854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Line Item  Revenue  Expenses  Profit\n",
       "2017Q1      115904     86544   29360\n",
       "2017Q2      120854     89485   31369\n",
       "2017Q3      118179     87484   30695\n",
       "2017Q4      130936     97743   33193\n",
       "2018Q1      117542     87688   29854"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.set_index(\"Line Item\",inplace=True)\n",
    "df = df.T\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Line Item</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Profit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017Q1</th>\n",
       "      <td>115904</td>\n",
       "      <td>86544</td>\n",
       "      <td>29360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q2</th>\n",
       "      <td>120854</td>\n",
       "      <td>89485</td>\n",
       "      <td>31369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q3</th>\n",
       "      <td>118179</td>\n",
       "      <td>87484</td>\n",
       "      <td>30695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q4</th>\n",
       "      <td>130936</td>\n",
       "      <td>97743</td>\n",
       "      <td>33193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018Q1</th>\n",
       "      <td>117542</td>\n",
       "      <td>87688</td>\n",
       "      <td>29854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Line Item  Revenue  Expenses  Profit\n",
       "2017Q1      115904     86544   29360\n",
       "2017Q2      120854     89485   31369\n",
       "2017Q3      118179     87484   30695\n",
       "2017Q4      130936     97743   33193\n",
       "2018Q1      117542     87688   29854"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index = pd.PeriodIndex(df.index, freq=\"Q-JAN\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1'], dtype='period[Q-JAN]', freq='Q-JAN')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-02-01 00:00:00')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index[0].start_time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4 style=\"color:green\">Add start date end date columns to dataframe</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Line Item</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Profit</th>\n",
       "      <th>Start Date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017Q1</th>\n",
       "      <td>115904</td>\n",
       "      <td>86544</td>\n",
       "      <td>29360</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q2</th>\n",
       "      <td>120854</td>\n",
       "      <td>89485</td>\n",
       "      <td>31369</td>\n",
       "      <td>2016-05-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q3</th>\n",
       "      <td>118179</td>\n",
       "      <td>87484</td>\n",
       "      <td>30695</td>\n",
       "      <td>2016-08-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q4</th>\n",
       "      <td>130936</td>\n",
       "      <td>97743</td>\n",
       "      <td>33193</td>\n",
       "      <td>2016-11-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018Q1</th>\n",
       "      <td>117542</td>\n",
       "      <td>87688</td>\n",
       "      <td>29854</td>\n",
       "      <td>2017-02-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Line Item  Revenue  Expenses  Profit Start Date\n",
       "2017Q1      115904     86544   29360 2016-02-01\n",
       "2017Q2      120854     89485   31369 2016-05-01\n",
       "2017Q3      118179     87484   30695 2016-08-01\n",
       "2017Q4      130936     97743   33193 2016-11-01\n",
       "2018Q1      117542     87688   29854 2017-02-01"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Start Date\"]=df.index.map(lambda x: x.start_time)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Line Item</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Expenses</th>\n",
       "      <th>Profit</th>\n",
       "      <th>Start Date</th>\n",
       "      <th>End Date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017Q1</th>\n",
       "      <td>115904</td>\n",
       "      <td>86544</td>\n",
       "      <td>29360</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-04-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q2</th>\n",
       "      <td>120854</td>\n",
       "      <td>89485</td>\n",
       "      <td>31369</td>\n",
       "      <td>2016-05-01</td>\n",
       "      <td>2016-07-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q3</th>\n",
       "      <td>118179</td>\n",
       "      <td>87484</td>\n",
       "      <td>30695</td>\n",
       "      <td>2016-08-01</td>\n",
       "      <td>2016-10-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017Q4</th>\n",
       "      <td>130936</td>\n",
       "      <td>97743</td>\n",
       "      <td>33193</td>\n",
       "      <td>2016-11-01</td>\n",
       "      <td>2017-01-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018Q1</th>\n",
       "      <td>117542</td>\n",
       "      <td>87688</td>\n",
       "      <td>29854</td>\n",
       "      <td>2017-02-01</td>\n",
       "      <td>2017-04-30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Line Item  Revenue  Expenses  Profit Start Date   End Date\n",
       "2017Q1      115904     86544   29360 2016-02-01 2016-04-30\n",
       "2017Q2      120854     89485   31369 2016-05-01 2016-07-31\n",
       "2017Q3      118179     87484   30695 2016-08-01 2016-10-31\n",
       "2017Q4      130936     97743   33193 2016-11-01 2017-01-31\n",
       "2018Q1      117542     87688   29854 2017-02-01 2017-04-30"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"End Date\"]=df.index.map(lambda x: x.end_time)\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
